var jude = Require(path.join(__dirname, '../../../component/verifty.js'));


var listEXItem = function (config, parent) {
    this.config = config;
    var html = "<div class='listItem' title=\"" + config.title + "\">" + config.title + (config.id == __DBID__ ? '&nbsp; (当前)' : '') + "</div>";
    this.HTML = $(html);
    this.parent = parent;
    this.events = {
        active: []
    };
    this.HTML.click(() => {
        this.active();
    });
    // this.HTML.bind("contextmenu", function (e) {
    //     console.log(0);
    // })

};
listEXItem.prototype.render = function () {
    return this.HTML;
};
listEXItem.prototype.on = function (str, event) {
    if (this.events.hasOwnProperty(str)) {
        this.events[str].push(event);
    };
};
listEXItem.prototype.active = function () {
    if (!this.HTML.hasClass('active')) {
        for (let i = 0; i < this.events.active.length; i++) {
            const element = this.events.active[i];
            element(this);
        };
        this.HTML.addClass('active');
    };
};
listEXItem.prototype.unActive = function () {
    this.HTML.removeClass('active');
};

var listEX = function () {
    var html = '<div class="list custom-scroll theme-scroll TA"></div>';
    this.items = {};
    this.HTML = $(html);
    this.events = {
        clear: [],
        remove: [],
    };
};
listEX.prototype.render = function () {
    return this.HTML;
};
listEX.prototype.unActive = function () {
    for (const key in this.items) {
        if (this.items.hasOwnProperty(key)) {
            const element = this.items[key];
            element.unActive();
        }
    }
};
listEX.prototype.remove = function (id) {
    var item = this.items[id];
    item.HTML.remove();
    delete this.items[id];
    this.unActive();
    for (let i = 0; i < this.events.remove.length; i++) {
        const element = this.events.remove[i];
        element(this);
    };
};
listEX.prototype.active = function (id) {
    this.items[id].active();
};
listEX.prototype.addItem = function (config, clickEvent, callback) {
    var data = new listEXItem(config, this);
    this.items[config.id] = data;
    data.on('active', (e) => {
        this.unActive();
        clickEvent(e);
    });
    if (callback) {
        callback(data.config);
    };
    this.HTML.prepend(data.render());
    return data;
};
listEX.prototype.append = function (data, clickEvent) {
    if (jude.type(data, 'object')) {
        this.addItem(data, clickEvent);
        return 'success';
    } else if (jude.type(data, 'array')) {
        this.clear();
        for (let i = 0; i < data.length; i++) {
            const element = data[i];
            this.addItem(element, clickEvent);
        };
        return 'success';
    } else {
        return 'error';
    }
};
listEX.prototype.clear = function () {
    for (let i = 0; i < this.events.clear.length; i++) {
        const element = this.events.clear[i];
        element(this);
    };
    this.HTML.empty();
    this.items = {};
};
listEX.prototype.on = function (str, event) {
    if (this.events.hasOwnProperty(str)) {
        this.events[str].push(event);
    };
};
listEX.prototype.upDateConfig = function (id, config) {
    var item = this.items[id];

    item.config.ip = config.ip;
    item.config.port = config.port;
    item.config.DBName = config.DBName;
    item.config.username = config.username;
    item.config.password = config.password;
    item.config.title = config.title;
    item.config.type = config.type;
    item.config.description = config.description;

    item.HTML.text(config.title).attr('title', config.title);
};
